﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Frw
{
    //public static class ExampleLogger
    //{
    //    #region Members
    //    /// <summary> 
    //    /// Create a logger for this static class
    //    /// </summary> 
    //    private static readonly ILog logger = LogManager.GetLogger(typeof(ExampleLogger));
    //    #endregion Members

    //    #region Constructors
    //    /// <summary> 
    //    /// On construct - build logger object 
    //    /// </summary> 
    //    static ExampleLogger()
    //    {
    //        XmlConfigurator.Configure();
    //    }
    //    #endregion Constructors

    //    #region Class Methods
    //    /// <summary> 
    //    /// Write information to log 
    //    /// </summary> 
    //    /// <param name="logLevel">Level of information to write</param> 
    //    /// <param name="message">Information to write to log</param> 
    //    public static void WriteLog(LogLevel logLevel, String message)
    //    {
    //        if (logLevel.Equals(LogLevel.Debug))
    //        {
    //            logger.Debug(message);
    //        }
    //        else if (logLevel.Equals(LogLevel.Error))
    //        {
    //            logger.Error(message);
    //        }
    //        else if (logLevel.Equals(LogLevel.Fatal))
    //        {
    //            logger.Fatal(message);
    //        }
    //        else if (logLevel.Equals(LogLevel.Info))
    //        {
    //            logger.Info(message);
    //        }
    //        else if (logLevel.Equals(LogLevel.Warn))
    //        {
    //            logger.Warn(message);
    //        }
    //    }
    //    #endregion Class Methods
    //}

    public class Logger : BusinessClassBase<ILogger, Logger>, ILogger
    {
        private string cs = "Frw";
        private EventLog elog = null;

        public Logger()
        {
            elog = new EventLog();
        }

        private void BuildUpEventLog()
        {
            if (!EventLog.SourceExists(cs))
            {
                EventLog.CreateEventSource(cs, cs);
            }

            elog.Source = cs;
            elog.EnableRaisingEvents = true;
        }

        public void Log(Exception exception)
        {
            BuildUpEventLog();

            elog.WriteEntry(exception.Message);
        }

        public void Log(LogLevel logLevel, string message)
        {
            BuildUpEventLog();

            switch (logLevel)
            {
                case LogLevel.Debug:
                    elog.WriteEntry(message, EventLogEntryType.Information);
                    break;
                case LogLevel.Error:
                    elog.WriteEntry(message, EventLogEntryType.Error);
                    break;
                case LogLevel.Fatal:
                    elog.WriteEntry(message, EventLogEntryType.Error);
                    break;
                case LogLevel.Info:
                    elog.WriteEntry(message, EventLogEntryType.Information);
                    break;
                case LogLevel.Warn:
                    elog.WriteEntry(message, EventLogEntryType.Warning);
                    break;
                default:
                    break;
            }
        }
    }
}
